#if 0
       int getopt_long(int argc, char * const argv[],
                  const char *optstring,
                  const struct option *longopts, int *longindex);

       int getopt_long_only(int argc, char * const argv[],
                  const char *optstring,
                  const struct option *longopts, int *longindex);

           struct option {
               const char *name;
               int         has_arg;
               int        *flag;
               int         val;
           };

       The meanings of the different fields are:

       name   is the name of the long option.

       has_arg
              is:  no_argument  (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argument; or optional_argument (or 2) if the
              option takes an optional argument.

       flag   specifies how results are returned for a long option.  If flag is NULL, then getopt_long() returns val.  (For example, the calling program may  set  val  to  the
              equivalent  short  option  character.)   Otherwise,  getopt_long()  returns 0, and flag points to a variable which is set to val if the option is found, but left
              unchanged if the option is not found.

       val    is the value to return, or to load into the variable pointed to by flag.

       The last element of the array has to be filled with zeros.

       If longindex is not NULL, it points to a variable which is set to the index of the long option relative to longopts.

       getopt_long_only() is like getopt_long(), but '-' as well as "--" can indicate a long option.  
       If an option that starts with '-' (not "--") doesnot match a long option
       but does match a short option, it is parsed as a short option instead.

RETURN VALUE
       If  an  option was successfully found, then getopt() returns the option character.  If all command-line options have been parsed, then getopt() returns -1.  If getopt()
       encounters an option character that was not in optstring, then '?' is returned.  If getopt() encounters an option with a missing argument, then the return value depends
       on the first character in optstring: if it is ':', then ':' is returned; otherwise '?' is returned.

       getopt_long() and getopt_long_only() also return the option character when a short option is recognized.  For a long option, they return val if flag is NULL, and 0 oth‐
       erwise.  Error and -1 returns are the same as for getopt(), plus '?' for an ambiguous match or an extraneous parameter.

#endif
#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>

int main(int argc, char **argv)
{
    int c;
    int digit_optind = 0;

    while (1) {
        int this_option_optind = optind ? optind : 1;
        int option_index = 0;
        static struct option long_options[] = {
            {"add",     1,  0,  0},
            {"append",  0,  0,  0},
            {"delete",  1,  0,  0},
            {"verbose", 0,  0,  0},
            {"create",  1,  0, 'c'},
            {"file",    1,  0,  0},
            {0, 0, 0, 0}
        };

        c = getopt_long(argc, argv, "", /*"abc:d:012",*/
                long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
            case 0:
            printf("long option %s", long_options[option_index].name);
            if (optarg)
                printf(" long option with arg %s", optarg);
            printf("\n");
            break;

            case '0':
            case '1':
            case '2':
            if (digit_optind != 0 && digit_optind != this_option_optind)
                printf("digits occur in two different argv-elements.\n");
            digit_optind = this_option_optind;
            printf("option %c\n", c);
            break;

            case 'a':
            printf("option a\n");
            break;

            case 'b':
            printf("option b\n");
            break;

            case 'c':
            printf("option c with value '%s'\n", optarg);
            break;

            case 'd':
            printf("option d with value '%s'\n", optarg);
            break;

            case '?':
            printf("case ?\n");
            break;

            default:
            printf("?? getopt returned character code 0%o ??\n", c);
        }
    }

    if (optind < argc) {
        printf("non-option ARGV-elements: ");
        while (optind < argc)
            printf("%s ", argv[optind++]);
        printf("\n");
    }

    exit(EXIT_SUCCESS);
}
